package ai.taromed.vr

import ai.taromed.vr.adapters.VideoInfoAdapter
import android.Manifest
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.recyclerview.widget.GridLayoutManager
import kotlinx.android.synthetic.main.activity_home.*
import java.io.File

class HomeActivity : AppCompatActivityEx() {

	private var videos = arrayListOf<LocalVideoBean>()

	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		requestWindowFeature(Window.FEATURE_NO_TITLE)
		setContentView(R.layout.activity_home)

		window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
		window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)



		supportActionBar?.hide()
		window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		window.decorView.systemUiVisibility = (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
				or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
				or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
				or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
				or View.SYSTEM_UI_FLAG_FULLSCREEN
				or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY)

		initVideos()
		initRc()

	}

	private fun requestPermission(){
		requestPermissions(
			arrayOf( Manifest.permission.READ_EXTERNAL_STORAGE,
				Manifest.permission.WRITE_EXTERNAL_STORAGE), object: PermissionCallback {
				override fun onPermissionGranted() {

				}
				override fun onPermissionDenied() {
				}
			}
		)
	}


	private fun openSetting(){
		var contentUri = Uri.parse("package:${packageName}")
		var mIntent = Intent("android.settings.APPLICATION_DETAILS_SETTINGS", contentUri) //打开应用信息页
		startActivity(mIntent)
	}


	private fun initVideos(){
		var basePath = Environment.getExternalStorageDirectory().absolutePath
		var localVideo1 = LocalVideoBean(0,"Factory\nEntrance","${basePath}/taromed/FactoryEntrance.mp4")
		var localVideo2 = LocalVideoBean(1,"Safty\nIntroduction","${basePath}/taromed/SaftyIntroduction.mp4")
		var localVideo3 = LocalVideoBean(2,"Taromed\nIntroduction","${basePath}/taromed/TaromedIntroduction.mp4")
		var localVideo4 = LocalVideoBean(3,"Warehouse","${basePath}/taromed/Warehouse.mp4")
		var localVideo5 = LocalVideoBean(4,"Manufacture Room","${basePath}/taromed/ManufactureRoom.mp4")
		var localVideo6 = LocalVideoBean(5,"Test Room","${basePath}/taromed/TestRoom.mp4")
		videos.add(localVideo1)
		videos.add(localVideo2)
		videos.add(localVideo3)
		videos.add(localVideo4)
		videos.add(localVideo5)
		videos.add(localVideo6)
	}

	private fun initRc(){

		val layoutManager = GridLayoutManager(this@HomeActivity, 3) //第二个参数为网格的列数
		rc_videos.layoutManager = layoutManager
		var adapter = VideoInfoAdapter(R.layout.layout_video_info_item , videos)
		adapter.setOnItemClickListener { adapter, view, position ->
			if (ActivityCompat.checkSelfPermission(this,  Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
				&& ActivityCompat.checkSelfPermission(this,  Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {

				var itemData = videos[position]
				start( menuIndex = itemData.index, filePath = itemData.filePath,)
			}else{
				requestPermission()
			}
		}
		rc_videos.adapter = adapter
		adapter.notifyDataSetChanged()
	}



	private fun start(
		menuIndex:Int,
		filePath: String,
		planeModeEnabled: Boolean = false,
		videoHotspotPath: String? = null
	) {
		if(!File(filePath).exists()){
			Toast.makeText(this@HomeActivity , "the video is not exist!" , Toast.LENGTH_SHORT).show()
			return
		}

		val configBundle: ConfigBundle = ConfigBundle
			.newInstance()
			.setMenuIndex(menuIndex)
			.setFilePath(filePath)
			.setPlaneModeEnabled(planeModeEnabled) //set it false to see default hotspot
			.setRemoveHotspot(true)
			.setVideoHotspotPath(videoHotspotPath)

		val intent = Intent(this, PlayerActivity::class.java)
		intent.putExtra(PlayerActivity.CONFIG_BUNDLE, configBundle)
		startActivity(intent)

	}






}
